#include "StdAfx.h"
#include  "crc.h"

BOOL GetFileCRC(const char *szFileName,DWORD &dwCRC)
{
	dwCRC = 0;

	HFILE hFile = _lopen(szFileName,OF_READ);
	if (hFile == HFILE_ERROR) 
	{
		return FALSE;
	}
	long nFileLen = _llseek(hFile,0,FILE_END);
	_llseek(hFile,0,FILE_BEGIN);

	//
	char *pBuf = (char*)malloc(10*1024);
	if (pBuf == NULL)
	{
		_lclose( hFile); 
		return FALSE;
	}

	//
	DWORD oldcrc32 = 0xFFFFFFFF;
	while(TRUE)
	{
		int nThisRead = min(10240,nFileLen);
		int nRead = _lread( hFile, pBuf, nThisRead );
		if (nRead <= 0)
		{
			break;
		}
		for(long i=0; i<nRead; i++)
		{ 
			char c = pBuf[i];
			oldcrc32 = UPDC32(c, oldcrc32);
		}
		nFileLen -= nRead;
		if (nFileLen <=0 )
		{
			break;
		}
	}

	//
    _lclose( hFile);       
	free(pBuf);
	dwCRC = oldcrc32;
                        
	return TRUE;
}
